home *** CD-ROM | disk | FTP | other *** search
/ Webshot XL / WEBSHOP_XL.ISO / WebShop XL / data1.cab / Examples / Web_Examples / AppForms / WebSite / CGI-BIN / CGI4.pl < prev    next >
Text File  |  2001-09-05  |  15KB  |  467 lines

  1. $cginame = "CGI4";
  2. $cgifolder = "";
  3. $pageURL = "../index.htm";
  4. $cgiurl = "\""."$cgifolder"."$cginame".".pl"."\"";
  5. $template = "Template1";
  6. ###############################################################
  7. #             1.Environmental prefix                          #
  8. ###############################################################
  9. #                  *-OS platform -*                           #
  10. $osplat = "Win32";
  11. #             *-DB connectivity platform-*                    #
  12. $dbplat = "ODBC";
  13. ###############################################################
  14. #             2.Section of datasource properies               #
  15. ###############################################################
  16. $dbname = "ADDBOOK";
  17. ###############################################################
  18. #              3.Section of columns                           #
  19. ###############################################################
  20. $command = SELECT;
  21. @columns = ( 'FullName','Country','City','Address','EmailAddress','TuitionPlan','Date');
  22. @captions = ( 'Name','Country','City','Address','Email Address','Tuition Plan','Visit date');
  23. @columntype = ( 'NONE','NONE','NONE','NONE','MAIL','NONE','DATE_0');
  24. ###############################################################
  25. #              4.Section of controls                          #
  26. ###############################################################
  27. @controls = ();
  28. @controlvalues = ();
  29. &get_control_values(@controls);
  30. ###############################################################
  31. #        5.Section of script body where    generated SQL      #
  32. #       statement template is to be placed with appropriate   #
  33. #                        insertions                           #
  34. ###############################################################
  35. $stmt0 = "SELECT Addresses.FullName,Addresses.Country,Addresses.City,Addresses.Address,Addresses.EmailAddress,Addresses.TuitionPlan,Addresses.Date\n
  36.  FROM Addresses\n
  37. ORDER BY  Date ASC";
  38.    ###############################################################
  39.    #              6.Section of feedback properies                #
  40.    ###############################################################
  41. @colors = ("d3d3d3","ffffff");
  42. $quota = 3;
  43. $style = 1;#0-List,1-Table
  44. $border = 1;
  45. $link = 0;#0-Link,1-Embed
  46. $head1 = "GuestBook Visitors List";  #first line
  47. $head2 = "";  #second line
  48. $align = "center";
  49. ##################### End of prefix ###########################
  50.  
  51. #$test = FALSE;
  52. #####################Standard remainder of the script##########
  53. $useplatstr = "use "."$osplat"."::"."$dbplat";
  54. eval $useplatstr;
  55. use CGI ':standard';
  56. $header = "Header";
  57.  
  58. #################################################################
  59. #        7.Section of script body responsible                   #
  60. #             for input from stdio                              #
  61. #        and output to stdio footer's lines                     #
  62. #################################################################
  63. print header;
  64. #print   "<center><U><B><FONT SIZE=\"+5\">SQL TEST</FONT></B></U><center>";
  65. if (param()) {
  66.  }
  67.  
  68. #################################################################
  69. #        8.Section of script body responsible                   #
  70. #           for placing controls' values                        #
  71. #      in appropriate places in "controlvalues" array           #
  72. #      and placing received cursor in $cursor variable          #
  73. #################################################################
  74. $cursor = param('Cursor');
  75.  
  76. #################################################################
  77. #        9.Section of script body responsible                   #
  78. #           for placing controls' values                        #
  79. #      in appropriate places in "controlvalues" array           #
  80. #      and placing received cursor in $cursor variable          #
  81. #################################################################
  82. if($test){
  83. print '<FORM ACTION=';
  84. print $cgiurl;
  85. print 'METHOD=POST>';
  86. $colindex = 0;
  87. foreach $control (@controls){
  88.  print '<CENTER>';
  89.  $temp = $controls[$colindex];
  90.  print $temp ;
  91.  print '</CENTER><BR>';    
  92.  print '<CENTER>';
  93.  print '<INPUT TYPE="text" NAME="';
  94.  print $control;
  95.  print '"';
  96.  print ' VALUE="';
  97.  $temp = $controlvalues[$colindex];
  98.  print $temp;
  99.  print '" >';
  100.  print '</CENTER>';    
  101.  print '<BR >';
  102.  $colindex = $colindex + 1;
  103. }
  104. if($colindex){
  105. print '<BR><BR>';
  106. print '<CENTER>';
  107. print '<INPUT TYPE="Submit" NAME="submit"';
  108. print '</CENTER>';
  109. print '</FORM>';
  110. }
  111. }
  112.  
  113. #################################################################
  114. #        10.Section of script body where    generated SQL        #
  115. #statement template is to be placed with appropriate insertions #
  116. #     and   responsible  for getting  final sight  of SQL       #
  117. ################################################################# 
  118. $stmtval = "\$stmt"."= \""."$stmt0"."\"";
  119. eval  $stmtval;
  120.  
  121. #################################################################
  122. #             11.Section of script body                          #
  123. #     responsible  for making connection to DBASE and           #
  124. #         getting count of rows                                 #
  125. ################################################################# 
  126. $plat = "\$db"." = "."new "."$osplat"."::"."$dbplat"."("."$dbname".")";
  127. eval $plat;
  128. if (!$db){
  129.  &print_db_error;
  130.  return;
  131. }
  132. $lor = $db->Sql($stmt);
  133. if ($lor){
  134.  &print_sql_error;
  135.  return;
  136. }
  137. $rowtotal = &get_total_rows;
  138.  
  139. #################################################################
  140. #             12.Section of script body                         #
  141. #        responsible  for output "Query results" line           #
  142. #             along with total count of rows                    #
  143. #################################################################
  144. #### only for debugging purpose
  145. if($test){
  146. print $stmtval,p;
  147. }
  148. print   "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%><TR VALIGN=\"top\" ALIGN=\"center\" bgcolor=\"#aaaacc\">";
  149. print   "<TD COLSPAN=2><H1> $head1</H1></TD></TR>";
  150. print   "<TR VALIGN=\"top\" ALIGN=\"center\" bgcolor=\"#aaaacc\">";
  151. print   "<TD COLSPAN=2><H1>$head2</H1></TD></TR>";
  152. print   "</TABLE>";
  153. print   "<center><U><B>QUERY RESULTS - $rowtotal matches</B></U><center>";
  154. print   "<BR><BR>";
  155.  
  156. #################################################################
  157. #             13.Section of script body                         #
  158. #        responsible  for calculation range of                  #
  159. #              query portion for feedback page                  #
  160. #################################################################
  161. $start = $cursor;
  162. $end = $cursor + $quota;
  163. if($end >= $rowtotal){
  164. $end = $rowtotal;
  165. $cursornext = 0;
  166. }
  167. else
  168. {
  169.  $cursornext = $cursor + $quota;
  170. $cursorprev = $cursor - $quota;
  171.  
  172. #################################################################
  173. #             14.Section of script body                         #
  174. #        responsible  for calculation range of                  #
  175. #                   query portion                               #
  176. #           output them in outgoing stream                      #
  177. #################################################################
  178. &print_range_line($rowtotal,$start,$end);
  179.  
  180. #################################################################
  181. #             15.Section of script body                         #
  182. #        responsible  for output of table captions              #
  183. #            for query columns on feedback page                 #
  184. #################################################################
  185.  print   "<TABLE BORDER=$border CELLSPACING=0 CELLPADDING=0 WIDTH=100%><TR VALIGN=\"top\" ALIGN=\"$align\" bgcolor=\"#aaaacc\">";
  186.  $capindex = 0;
  187.  @fieldnames = $db->FieldNames();
  188.  foreach $caption (@captions){
  189.  if($caption eq ""){
  190.   &print_header_cell($fieldnames[$capindex]);
  191.  } else{
  192.   &print_header_cell($captions[$capindex]);
  193.  }$capindex = $capindex + 1;
  194.  }
  195.  
  196. #################################################################
  197. #             16.Section of script body                         #
  198. #        responsible  for fetching rows from DBASE              #
  199. #            and placing values in the cells of table           #
  200. #################################################################
  201. $lor = $db->Sql($stmt);
  202. $j=0;
  203. while($db->FetchRow()){
  204.  if($j < $start ){
  205.   $j++;
  206.   next;
  207.  }
  208.  if($j >= $end ){
  209.  last;
  210.  }
  211.  undef %hash;
  212.  %hash = $db->DataHash;
  213.  $bgcolor = $colors[$j%2];
  214.  print "<TR VALIGN=\"top\" ALIGN=\"$align\" bgcolor=\"$bgcolor\">";
  215.  $i=0;
  216.  foreach $column (@columns){
  217.   if($columntype[$i] EQ "NONE"){
  218.    &print_cell($hash{$column});
  219.   }
  220.   else{
  221.     $_=$columntype[$i];
  222.    if($columntype[$i] EQ "URL"){
  223.     &print_url_cell($hash{$column});
  224.    }
  225.    if($columntype[$i] EQ "MAIL"){
  226.     &print_mail_cell($hash{$column});
  227.    }
  228.    if($columntype[$i] EQ "IMAGE"){
  229.     &print_image_cell($hash{$column});
  230.    }
  231.     if($columntype[$i] EQ "BOOL"){
  232.     &print_bool_cell($hash{$column});
  233.    }
  234.    if(/^DATE_(\d+)$/){
  235.    $datform=$1;
  236.    &print_date_cell($hash{$column},$datform);
  237.    }
  238.    if($columntype[$i] EQ "TIME"){
  239.    &print_time_cell($hash{$column});
  240.    }
  241.   }
  242.   $i = $i + 1;
  243.  }
  244.  $j = $j + 1;    
  245. }
  246. print "</TABLE>";
  247. print   "<BR>";
  248. print   "<BR>";
  249. #### end of session ##########
  250. $db->Close();
  251.  
  252. #################################################################
  253. #             17.Section of script body   (NEXT)                #
  254. #        responsible  for generation and placing in             #
  255. #            output stream the form tags as invisible           # 
  256. #     fields for getting query information from feedback page   #
  257. #################################################################
  258. #if it is needed to place NEXT button
  259. if($cursornext > 0){
  260.  print '<FORM ACTION=';
  261.  print $cgiurl;
  262.  print 'METHOD=POST>';
  263.  $colindex = 0;
  264.  foreach $control (@controls){
  265.   &print_field("hidden",$control,$controlvalues[$colindex]);  
  266.   $colindex = $colindex + 1;
  267.  }
  268.  &print_field("hidden","Cursor",$cursornext);
  269.  &print_field("Submit","","Next");
  270.  print "</FORM>";
  271. }
  272.  
  273. #################################################################
  274. #             18.Section of script body   (PREVIOUS)            #
  275. #        responsible  for generation and placing in             #
  276. #            output stream the form tags as invisible           # 
  277. #     fields for getting query information from feedback page   #
  278. #################################################################
  279. #if it is needed to place PREVIOUS button
  280. if($cursorprev >= 0){
  281.  print '<FORM ACTION=';
  282.  print $cgiurl;
  283.  print 'METHOD=POST>';
  284.  $colindex = 0;
  285.  foreach $control (@controls){
  286.  &print_field("hidden",$control,$controlvalues[$colindex]);  
  287.  $colindex = $colindex + 1;
  288. }
  289.  &print_field("hidden","Cursor",$cursorprev);
  290.  &print_field("Submit","Submit","Previous");
  291.  print "</FORM>";
  292. }
  293. if($pageURL NE ""){
  294.  print "<BR><A HREF =\"$pageURL\" TARGET=\"_top\">Go Back</A>";
  295. }
  296. print end_html;
  297. ###
  298.  
  299. sub print_field
  300. {
  301.     local($type, $name, $value) = @_;
  302.     print '<INPUT TYPE="';
  303.     print  $type;
  304.     print  '" NAME="';
  305.     print  $name;
  306.     print  '" VALUE="';
  307.     print  $value;
  308.     print  '">';
  309. }
  310. sub print_image_cell
  311. {
  312.  local($name) = @_;
  313.  $name = &get_converted_string($name);
  314.  print "<TD COLSPAN=1><P ALIGN = $align>";
  315.  if(!$link){
  316.  print "<IMG  SRC=\"$name\"  BORDER=0  ALT=\"$name\"></P></TD>";
  317.  }
  318.  else{
  319.   print "<A HREF =\"$name\"><font size=-1 face=arial>$name</font></A></P></TD>";
  320.  }
  321. }
  322. sub print_date_cell
  323. {
  324. local($datetime,$datformat) = @_;
  325. ($date,$time)=split(' ',$datetime);
  326. $date = &get_converted_string($date);
  327. $wholestring = &format_date($date,$datformat);
  328. &print_cell($wholestring);
  329. }
  330. sub print_time_cell
  331. {
  332. local($datetime) = @_;
  333. ($date,$time)=split(' ',$datetime);
  334. &print_cell($time);
  335. }
  336. sub print_mail_cell
  337. {
  338.  local($name) = @_;
  339.  $name = &get_converted_string($name);
  340.  print    "<TD COLSPAN=1><P ALIGN = $align>";
  341.  print "<A HREF =\"mailto:$name\"><font size=-1 face=arial>$name</font></A></P></TD>";
  342. }
  343. sub print_url_cell
  344. {
  345.  local($name) = @_;
  346.  $name = &get_converted_string($name);
  347.  print    "<TD COLSPAN=1><P ALIGN = $align>";
  348.  print "<A HREF =\"$name\"><font size=-1 face=arial>$name</font></A></P></TD>";
  349. }
  350. sub print_cell
  351. {
  352.  local($name) = @_;
  353.  $name = &get_converted_string($name);
  354.  print    "<TD COLSPAN=1><P ALIGN = $align>";
  355.  print "<font size=-1 face=arial>$name</font></P></TD>";        
  356. }
  357. sub print_header_cell
  358. {
  359.  local($name) = @_;
  360.  print    "<TD COLSPAN=1><P ALIGN = $align>";
  361.  print "<B><font size=+1 face=arial>$name</font></B></P></TD>";        
  362. }
  363. sub print_range_line
  364. {
  365.  local($rowtotal,$start,$end) = @_;
  366. if($rowtotal){
  367.  print   "<center>From ";
  368.  print eval($start +1);
  369.  print    " to ";
  370.  print    $end;
  371.  print    "</center>";
  372.  print   "<BR>";
  373. }
  374. }
  375.  
  376. sub get_converted_string
  377. {
  378.   local($name) =@_;
  379.   $name=~ s/&/&/g;
  380.   $name=~ s/</</g;
  381.   $name=~ s/>/>/g;
  382.   $name=~ s/⌐/©/g;
  383.   $name=~ s/«/®/g;
  384.   $name=~ s/"/"/g;
  385.   $name=~ s/\n/<BR>/g;
  386.   $name=~ s/''/'/g;
  387.   return $name;
  388. }
  389. sub get_control_values
  390. {
  391.  local(@controls)=@_;
  392.  local($cindex) = 0;
  393.  foreach $control (@controls){
  394.  if(($t=param($control))){
  395.    if($controlvalues[$cindex] EQ 'NO'){
  396.     $controlvalues[$cindex] = 'YES';
  397.    }
  398.    if($controlvalues[$cindex] EQ ''){
  399.     $t=~ s/'/''/g;
  400.     $t=~ s/\|//g;
  401.     $controlvalues[$cindex] =$t;
  402.    }
  403.  }
  404.  $cindex = $cindex + 1;
  405. }
  406. }
  407. sub format_date
  408. {
  409.  @months =('January','February','March','April','May','June','July','August','September','October','November','December');
  410.  @mon = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  411.  #dd-MMM-yyyy = 0
  412.  #MM/dd/yyyy = 1
  413.  #dd MMMM,yyyy = 2
  414.  #MMMM dd,yyyy = 3 
  415.  local($date,$dataformat) = @_;
  416.  ($y,$m,$d)=split('-',$date);
  417.  if($dataformat == "0"){
  418.   $m = $m -1;
  419.   return "$d $months[$m],$y"
  420.  }
  421.  if($dataformat == "1"){
  422.   return "$d-$mon[$m]-$y";
  423.  }
  424.  if($dataformat == "2"){
  425.   $m = $m -1;
  426.   return "$m/$d/$y";
  427.  }
  428.  if($dataformat == "3"){
  429.   $m = $m -1;
  430.   return "$months[$m] $d,$y";
  431.  }
  432. }
  433. sub print_bool_cell
  434. {
  435.   local($name) = @_;
  436.   if($name == "0"){
  437.     &print_cell("NO");
  438.   }
  439.   else{
  440.   &print_cell("YES");
  441.   }
  442. }
  443. sub get_total_rows
  444. {
  445. local($i)=0;
  446. while($db->FetchRow()){
  447.  $i = $i+1;
  448. }
  449. local($rowtotal) = $i;
  450. return $i;
  451. }
  452.  
  453. sub print_db_error
  454. {
  455. print "<H1>DATASOURCE OPEN ERROR:</H1>";
  456. print "<P>No such DSN: <u>$dbname</u></P>";
  457. }
  458. sub print_sql_error
  459. {
  460.  print "<H1>QUERY EXECUTION ERROR:</H1>";
  461.  print "<P>QUERY STATEMENT: <U>$stmt</U></P>";
  462.  print "<P>DIAGNOSTICS: <U>";
  463.  print $db->Error();
  464.  print "</U></P>";
  465. }
  466.